<?php
	require_once('paths.inc');
	require_once(INCLUDE_PATH.'constants.inc');
	require_once('page.inc');
	require_once(DB_PATH.'db.inc');
	
	require_once('security.inc');
	require_once('utils.inc');	
	require_once('time.inc');
	
	session_start();
	check_permissions();	
	checkIsLoggedIn();	

	if (isset($_POST['post_schimba_parola'])) {
        
		$db = db_connect();
		$parola_existenta = $_POST['parola_existenta'];
		$parola_noua = $_POST['parola_noua'];	
		$parola_noua2 = $_POST['parola_noua2'];
		
		if (!($parola_noua == $parola_noua2 && strlen($parola_noua) > 4))
			error_msg("LANG_Parola_introdusa_gresit_doua_ori", $_SERVER['PHP_SELF']);
			
		$user = $_SESSION['user_logat'];
		if ($user->checkPassword($parola_existenta)) {
			$user->password = $parola_noua;
			try {
				$user->queryChangePassword($db);
			}
			catch (QueryUpdateUserException $e) {
				redirectOnError(CT_CHANGE_PASS_UPDATE_USER);
			}
			catch (QueryWrongVersionException $e) {
				//redirectOnError(CT_CHANGE_PASS_WRONG_VERSION); ????
			}
            notice_msg(LANG_Password_Changed_Success, "home.php");
		}
		else {
			error_msg("LANG_Parola_Nu_E_Buna", $_SERVER['PHP_SELF']);
		}
	}
    
    html_header("LANG_Schimba_parola", null, "change_pass");
    $linkuri = array(LANG_Home => "home.php", LANG_Change_password_Title => "");
    titlu_pagina($linkuri);
	
    output_error_msg();
    output_notice_msg();
    output_extra_text(LANG_EXTRA_CHANGE_PASSWORD);
    
	?>
	<script language="Javascript">
		<!--
		function checkPass() {
			if (document.schimba_parola.parola_noua.value != document.schimba_parola.parola_noua2.value) {
				alert(LANG_PAROLELE_TREBUIESC_SA_FIE_IDENTICE);
				return false;
			}
			return true;
		}
		-->
	</script>	
	<div class="container" align="left">
		<form name="schimba_parola" action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post" onsubmit="javascript: 
						return checkPass();">	
			<table border="0" align="left" id="wrapper2">
				<thead>
					<tr>
						<th colspan="2" align="center"><?php echo LANG_Change_password; ?></th>
					</tr>
				</thead>
		 		<tr class="capTabel">
			 		<td align="right"><label for="parola_existenta"><?php echo LANG_PAROLA_EXISTENTA; ?></label></td>
			 		<td>
						<input type="password" id="parola_existenta" name="parola_existenta" maxlength="50" value=""/>					
					</td>
		 		</tr>
		 		<tr class="capTabel">
			 		<td align="right"><label for="parola_noua"><?php echo LANG_PAROLA_NOUA; ?></label></td>
			 		<td>
						<input type="password" id="parola_noua" name="parola_noua" maxlength="50" value=""/>					
					</td>
		 		</tr>
		 		<tr class="capTabel">
			 		<td align="right"><label for="parola_noua2"><?php echo LANG_PAROLA_NOUA_AGAIN; ?></label></td>
			 		<td>
						<input type="password" id="parola_noua2" name="parola_noua2" maxlength="50" value=""/>					
					</td>
		 		</tr>
		 		<tr class="capTabel">
		 			<td colspan="2" align="center">
		 				<?php buton_final_ok(LANG_Change_password, "post_schimba_parola"); ?>
		 				&nbsp;&nbsp;
		 				<?php buton_final_cancel(LANG_Cancel, "home.php"); ?>
		 			</td>
		 		</tr>
	 		</table>
 		</form>
 	</div>
	
	<?php
	
	html_footer();
?>